﻿// -----------------------------------------------------------------------
// <copyright file="CqrsBindingsTests.cs">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace Moveax.DataAccess.Cqrs.Highway.UnitTests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Linq;
    using System.Reflection;

    [TestClass]
    public class CqrsBindingsTests
    {
        [TestMethod]
        public void CoreBindQueries()
        {
            // Arrange
            var modelsAssembly = Assembly.Load("Moveax.DataAccess.Cqrs.Highway.UnitTests.Model");
            var dalAssembly = Assembly.Load("Moveax.DataAccess.Cqrs.Highway.UnitTests.DataAccess");
            var bindings = new CqrsBindings();

            // Act
            bindings.CollectQueryInterfaces(modelsAssembly);
            bindings.BindQueries(dalAssembly);

            // Assert
            Assert.IsTrue(bindings.Validate());
            Assert.IsNotNull(bindings.QueryBindings);
            Assert.IsTrue(bindings.QueryBindings.Any());

            foreach (var query in bindings.QueryBindings)
                Assert.IsTrue(query.Interface.IsAssignableFrom(query.Implementation));
        }

        [TestMethod]
        public void CoreBindCommands()
        {
            // Arrange
            var modelsAssembly = Assembly.Load("Moveax.DataAccess.Cqrs.Highway.UnitTests.Model");
            var dalAssembly = Assembly.Load("Moveax.DataAccess.Cqrs.Highway.UnitTests.DataAccess");
            var bindings = new CqrsBindings();

            // Act
            bindings.CollectCommands(modelsAssembly);
            bindings.BindCommandHandlers(dalAssembly);

            // Assert
            Assert.IsTrue(bindings.Validate());
            Assert.IsNotNull(bindings.CommandBindings);
            Assert.IsTrue(bindings.CommandBindings.Any());

            foreach (var command in bindings.CommandBindings) {
                foreach (var handler in command.Handlers) {
                    var genType = typeof(ICommandHandler<>).MakeGenericType(command.Command);
                    Assert.IsTrue(genType.IsAssignableFrom(handler));
                }
            }
        }
    }
}
